from rest_framework import serializers
from .models import CourseCategory

class CourseCategoryModelSerializer(serializers.ModelSerializer):
    """课程分类序列化器 都是查询数据不涉及修改"""
    class Meta:
        model = CourseCategory  #继承模型
        fields = ["id","name",]   #返回展示字段



# 在之前基础上新增返回字段 老师头像和详细信息
from .models import Teacher
class TeacherModelSerializer(serializers.ModelSerializer):
    """
        老师信息序列化器,由于外键指向teacher表，所以另一个序列化器引用这个teacher序列化器
    """
    class Meta:
        model = Teacher
        # 返回teacher表信息
        fields = ["id","image","name","title","signature","brief"]



from .models import Course
class CourseModelSerializer(serializers.ModelSerializer):
    """课程信息序列化器"""
    """
        teacher__name： Course表是teacher id，关联外键teacher表的name字段 ，返回teacher__name会报错
        所以序列化器查外键引用另一张表数据，需要嵌套序列化器
    """
    # 返回外键的值，外键单独弄序列化器，实例化
    teacher = TeacherModelSerializer()  # 如果老师是多个值，参数+ many=True
    class Meta:
        model = Course
        #返回数据库字段信息 为了外键teacher_id对应到teacher表的name需要给teacher再写一个序列化器，所以teacher实例化，"teacher"传给这个序列化器
        fields = ["id","name","course_img","students","lessons","pub_lessons","price","teacher","lessons_list","discount_name","real_price"]




class CourseRetrieveSerializer(serializers.ModelSerializer):
    """课程详情页序列化器"""
    teacher = TeacherModelSerializer()
    class Meta:
        model = Course
        fields = ["id", "name", "course_img", "students", "lessons", "pub_lessons", "price", "teacher", "level_name", "brief_html","course_video","discount_name","real_price","activity_time"]



from .models import CourseLesson
class CourseLessonSerializer(serializers.ModelSerializer):
    """
        嵌套在 详情页课程章节列表 序列化器中被引用
    """
    class Meta:
        model = CourseLesson
        fields = ["id","name","lession","duration","free_trail"]

from .models import CourseChapter
class CourseChapterSerializer(serializers.ModelSerializer):
    """
        详情页课程章节列表
        涉及外键信息，需要序列化器嵌套
    """
    coursesections = CourseLessonSerializer(many=True)  #序列化多个课时，增加many=True
    class Meta:
        model = CourseChapter
        fields = ["id","chapter", "name", "summary", "coursesections"]





















